package com.bsg.upm.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.quartz.CronExpression;
import org.springframework.stereotype.Service;

import com.bsg.upm.entity.BackupStrategyEntity;
import com.bsg.upm.service.BaseService;
import com.bsg.upm.service.ServService;
import com.bsg.upm.util.PropertyPlaceholder;

@Service
public class BackupStrategyMonitorTask extends BaseService {

	@Resource
	private ServService servService;

	public void doBackup() {
		try {
			logger.info("备份策略监控");
			List<BackupStrategyEntity> backupStrategys = backupStrategyDao.listEnabled();
			for (BackupStrategyEntity backupStrategy : backupStrategys) {
				if (backupStrategy.getEnabled() == true) {
					Date date = systemDao.getCurrentDateTime();

					// 备份策略过期时间
					Date expireDatetime = backupStrategy.getExpireDatetime();
					if (expireDatetime == null || expireDatetime.getTime() > date.getTime()) {
						// 备份策略监控频率
						int repeatInterval = Integer
								.parseInt(PropertyPlaceholder.getProperty("task.repeatInterval.backupStrategyMonitor"));

						String linuxCronExpression = backupStrategy.getCronExpression();
						String quartzCronExpression = convertQuartzCronExpression(linuxCronExpression);
						CronExpression cronExpression = new CronExpression(quartzCronExpression);
						// 下次备份时间
						Date nextBackupDatetime = cronExpression
								.getTimeAfter(new Date(systemDao.getCurrentDateTime().getTime() + repeatInterval));
						long diff = date.getTime() - nextBackupDatetime.getTime();
						if (diff >= 0 && diff <= repeatInterval) {
							try {
								logger.info("自动备份");
								Map<String, Object> paramMap = new HashMap<>();
								paramMap.put("servId", backupStrategy.getServ().getId());
								paramMap.put("type", backupStrategy.getType());
								paramMap.put("retention", backupStrategy.getRetention());
								paramMap.put("remark", "自动备份");
								servService.backup(paramMap, true);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String convertQuartzCronExpression(String linuxCronExpression) {
		String cronExpression = "";
		String[] arr = linuxCronExpression.split(" ");
		if (arr.length != 5) {
			return null;
		}
		if (arr[4].equals("*")) {
			arr[4] = "?";
		} else {
			switch (arr[4]) {
			case "0":
				arr[4] = "1";
				break;
			case "1":
				arr[4] = "2";
				break;
			case "2":
				arr[4] = "3";
				break;
			case "3":
				arr[4] = "4";
				break;
			case "4":
				arr[4] = "5";
				break;
			case "5":
				arr[4] = "6";
				break;
			case "6":
				arr[4] = "7";
				break;
			case "7":
				arr[4] = "1";
				break;
			default:
				break;
			}
			if (arr[2].equals("*")) {
				arr[2] = "?";
			}
		}
		for (String str : arr) {
			cronExpression += " " + str;
		}
		cronExpression = 0 + cronExpression;
		return cronExpression;
	}

	// public static void main(String[] args) throws ParseException {
	// BackupStrategyMonitorTask backupStrategyMonitorTask = new
	// BackupStrategyMonitorTask();
	// String linuxCronExpression = "3 8 * * 2";
	// String cron =
	// backupStrategyMonitorTask.convertQuartzCronExpression(linuxCronExpression);
	// System.out.println(cron);
	// CronExpression cronExpression = new CronExpression(cron);
	// System.out.println(cronExpression.getNextValidTimeAfter(new Date()));
	// }
}
